import LoginELview from '../views/LoginELview.vue'
import LoginView from '../views/LoginView.vue'
import LoginhuihuaView from '../views/LoginhuihuaView.vue'
//import App from '../App.vue'
import { createRouter, createWebHistory } from 'vue-router'
//import Dashboard from '../views/Dashboard.vue'
const routes = [
  {
    path: '/LoginView',
    name: 'LoginView',
    component:LoginView
  },
  {
    path: '/LoginhuihuaView',
    name: 'LoginhuihuaView',
    component:LoginhuihuaView,
     meta: { 
      requiresAuth: true // 标记为需要登录才能访问
    }
  },
  {
    path: '/LoginELview',
    name: 'LoginELview',
    component:LoginELview
  },
  {
    path:'/',
    redirect:'/LoginView'
  },
  {
    path: '/Forgot-password',
    name: 'ForgotPassword',
    component: () => import('@/views/ForgotPasswordView.vue')
  }

]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
// 路由守卫：未登录用户禁止访问需要授权的页面
router.beforeEach((to, from, next) => {
  // 检查目标页面是否需要登录
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 从 sessionStorage 中获取登录状态
    const user = sessionStorage.getItem('user')
    if (!user) {
      // 未登录，强制跳转到登录页
      next('/LoginELview')
    } else {
      // 已登录，允许访问
      next()
    }
  } else {
    // 不需要登录的页面（如登录页），直接放行
    next()
  }
})

export default router    